package com.gaogzhen.security.lock.reentrant;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 测试ReentrantLock 锁超时-tryLock无参
 */
@Slf4j(topic = "c.TestTryLock01")
public class TestTryLock01 {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            if(!lock.tryLock()) {
                log.debug("没有获取锁，退出");
                return;
            }
            try {
                log.debug("获取到锁，执行后续代码。。。");
            }finally {
                lock.unlock();
            }
        }, "t1");

        // 主线程先获取了锁
        lock.lock();

        t1.start();
    }
}
